package com.hand.wfive.ad;

import android.content.Context;
import android.content.SharedPreferences;

import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTCustomController;
import com.hand.wfive.App;
import com.hand.wfive.R;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 可以用一个单例来保存TTAdManager实例，在需要初始化sdk的时候调用
 */
public class TTAdManagerHolder {

    private static boolean mInit = false;

    public static boolean isInitAndReady() {
        return mInit && TTAdSdk.isSdkReady();
    }

    public static TTAdManager get() {
        return TTAdSdk.getAdManager();
    }

    public static void init(TTAdSdk.Callback initCallback) {
        doInit(initCallback);
    }

    //step1:接入网盟广告sdk的初始化操作，详情见接入文档和穿山甲平台说明
    private static void doInit(TTAdSdk.Callback initCallback) {
        if (!mInit) {
            mInit = TTAdSdk.init(App.getContext(), buildConfig());
        }
        doStart(initCallback);
    }

    private static void doStart(TTAdSdk.Callback initCallback) {
        if (TTAdSdk.isSdkReady()) {
            if (initCallback != null) {
                initCallback.success();
            }
        } else {
            TTAdSdk.start(new TTAdSdk.Callback() {
                @Override
                public void success() {
                    if (initCallback != null) {
                        initCallback.success();
                    }
                }

                @Override
                public void fail(int i, String s) {
                    if (initCallback != null) {
                        initCallback.fail(i, s);
                    }
                }
            });
        }
    }

    private static TTAdConfig buildConfig() {
        return new TTAdConfig.Builder()
                .appId(AdConfig.getAdAppId())
                .directDownloadNetworkType()
                .data(getData(getNoticeState()))
                .useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView
                .allowShowNotify(true) //是否允许sdk展示通知栏提示
                .debug(false) //测试阶段打开，可以通过日志排查问题，上线时去除该调用
                .supportMultiProcess(false)//是否支持多进程
                .customController(new TTCustomController() {
                    @Override
                    public boolean isCanUsePhoneState() {
                        return false;
                    }

                    @Override
                    public boolean isCanUseWriteExternal() {
                        return false;
                    }

                    @Override
                    public boolean isCanUseAndroidId() {
                        return false;
                    }

                    @Override
                    public boolean alist() {
                        return false;
                    }
                })
                .build();
    }

    /**
     * @param isOpen 个性化推荐广告开关
     */
    public static void updateNotice(boolean isOpen) {
        SharedPreferences sp = App.getContext().getSharedPreferences("ADNotice", Context.MODE_PRIVATE);
        sp.edit().putBoolean("isOpen", isOpen).apply();
        TTAdConfig ttAdConfig = new TTAdConfig.Builder()
                .data(getData(isOpen))
                .build();
        TTAdSdk.updateAdConfig(ttAdConfig);
    }

    public static boolean getNoticeState() {
        SharedPreferences sp = App.getContext().getSharedPreferences("ADNotice", Context.MODE_PRIVATE);
        return sp.getBoolean("isOpen", true);
    }

    private static String getData(boolean isOpen) {
        try {
            JSONArray jsonArray = new JSONArray();
            JSONObject personalObject = new JSONObject();
            personalObject.put("name", "personal_ads_type");
            personalObject.put("value", isOpen ? "1" : "0");
            jsonArray.put(personalObject);
            return jsonArray.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Boolean opNoticeState() {
        String channel = App.getContext().getString(R.string.channel);
        return "vivo".equals(channel) || "xiaomi".equals(channel) || "baidu".equals(channel);
    }
}
